package net.orange.framework.validator;

import net.orange.architecture.process.ProcessFactory;
import net.orange.architecture.validator.IObjectValidator;
import net.orange.framework.logic.ICommoninfoProcess;
import net.orange.framework.model.Commoninfo;

/**
 * Validation : Common info can't be duplicated. It means there is only
 * exclusive code in one common info type.
 */
public class CommoninfoDuplicateValidator extends FrameworkValidator implements
		IObjectValidator {
	/**
	 * The target common info object.
	 */
	private Commoninfo commoninfo;

	/**
	 * Disabled the default contractor.
	 */
	@SuppressWarnings("unused")
	private CommoninfoDuplicateValidator() throws Exception {

	}

	/**
	 * Constructor with common info object and DAO object to initialize the
	 * validation.
	 * 
	 * @param commoninfo
	 *            The target common info object
	 * @throws Exception
	 */
	public CommoninfoDuplicateValidator(Commoninfo commoninfo) throws Exception {
		super();
		this.commoninfo = commoninfo;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.validator.ObjectValidator#check()
	 */
	public boolean check() throws Exception {
		ICommoninfoProcess process = (ICommoninfoProcess) ProcessFactory
				.get(Commoninfo.class);
		System.out.println("1111====");
		Commoninfo other = (Commoninfo) process.get(commoninfo.getType(),
				commoninfo.getCode());
		System.out.println("2222====");
		return (other == null || other.getPk() == commoninfo.getPk());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.validator.ObjectValidator#getMessage()
	 */
	public String getMessage() {
		return message.get("error2_comm_01");
	}
}
